package com.nutiteq.location;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;

import com.nutiteq.MapComponent;
import com.nutiteq.components.PlaceIcon;
import com.nutiteq.components.WgsPoint;

public class TestSourceSettings extends Form implements CommandListener {

  private final MapComponent mapComponet;
  private final GPSCanvas canvas;

  private final TextField startLon;
  private final TextField startLat;
  private final TextField lonChange;
  private final TextField latChange;
  private final TextField updateInterval;
  private final ChoiceGroup track;

  private final Command ok = new Command("OK", Command.OK, 0);
  private final Command back = new Command("Back", Command.BACK, 0);
  private final WgsPoint originalMiddle;
  private final Image gpsImage;

  public TestSourceSettings(final MapComponent mapComponet, final GPSCanvas canvas,
      final Image gpsImage) {
    super("Test GPS");
    this.mapComponet = mapComponet;
    this.canvas = canvas;
    this.gpsImage = gpsImage;
    originalMiddle = mapComponet.getMiddlePoint();
    startLon = new TextField("Start lon", Double.toString(originalMiddle.getLon()), 10,
        TextField.ANY);
    startLat = new TextField("Start lat", Double.toString(originalMiddle.getLat()), 10,
        TextField.ANY);
    lonChange = new TextField("Lon move", "0.001", 6, TextField.ANY);
    latChange = new TextField("Lat move", "0.001", 6, TextField.ANY);
    updateInterval = new TextField("Update interval (ms)", "1000", 6, TextField.ANY);
    track = new ChoiceGroup("Track GPS", ChoiceGroup.POPUP);
    track.append("Yes", null);
    track.append("No", null);

    append(startLon);
    append(startLat);
    append(lonChange);
    append(latChange);
    append(updateInterval);
    append(track);

    addCommand(ok);
    addCommand(back);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == back) {
      canvas.show(canvas);
    } else if (cmd == ok) {
      final double sLon = ensureDouble(startLon.getString(), originalMiddle.getLon());
      final double sLat = ensureDouble(startLat.getString(), originalMiddle.getLat());
      final double lonChan = ensureDouble(lonChange.getString(), 0.001);
      final double latChan = ensureDouble(latChange.getString(), 0.001);
      final int updateTime = ensureInt(updateInterval.getString(), 1000);
      final int selectedTrackOption = track.getSelectedIndex();

      final LocationMarker marker = new NutiteqLocationMarker(new PlaceIcon(gpsImage, 4, 16),
          updateTime, selectedTrackOption == 0);
      final LocationSource source = new TestGPSSource(new WgsPoint(sLon, sLat), lonChan, latChan);
      source.setLocationMarker(marker);
      mapComponet.setLocationSource(source);
      canvas.addCommand(canvas.removeGPS);
      canvas.show(canvas);
    }
  }

  private int ensureInt(final String string, final int defaultValue) {
    try {
      return Integer.parseInt(string);
    } catch (final NumberFormatException e) {
      return defaultValue;
    }
  }

  private double ensureDouble(final String string, final double defaultValue) {
    try {
      return Double.parseDouble(string);
    } catch (final NumberFormatException e) {
      return defaultValue;
    }
  }
}
